/************************************************************
 ********************** 左表格*******************************
 ***********************************************************/
/**
/**
 * 检验项目标准字典列表初始化
 */
var SdsStandDicSub = {
    id: "dicSubTree",	//表格id
    seItem: null,		                //选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    dicSubId: 0,
    dicId: 0,
    pId: 0
};

/**
 * 根据标准化字典子项id查找关联检验项目
 */
SdsStandDicSub.onClickDicSub = function (e, treeId, treeNode) {
    if (treeNode.pId == null) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体字典。");
        return false;
    }
    SdsStandDicSub.dicSubId = "";
    SdsStandDicSub.dicId = "";
    SdsStandDicSub.pId = "";
    console.log(treeNode)
    if (treeNode.level == "1"){
        //标准化字典,加载检验医嘱
        SdsStandDicSub.dicId = treeNode.id;
        //查询标准化_检验医嘱
        SdsStandLaborderMatch.seItem = null;
        SdsStandDicSub.dicSubId = 0;
        var compare = "match";
        SdsStandLabitemMatch.search(compare);
        SdsStandLaborderMatch.search(compare);
    }else {
        //标准化字典子项，加载检验项目和
        SdsStandDicSub.dicSubId = treeNode.id;
        SdsStandDicSub.dicId = treeNode.pId;
        //查询标准化_检验项目
        SdsStandLabitemMatch.seItem = null;
        var compare = "match";
        SdsStandLabitemMatch.search(compare);
        SdsStandLaborderMatch.search(compare);
    }
}

/************************************************************
 ********************** 右上表格部分*******************************
 ***********************************************************/
/**
 */

/**
 * 标准化_检验医嘱配置管理初始化
 */
var SdsStandLaborderMatch = {
    id: "SdsStandLaborderMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsStandLaborderMatch.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter : selectcheckbox},
        {title: '检验医嘱', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '医嘱项', field: 'orderMastDesc', align: 'center', valign: 'middle', sortable: true},
    ];
    function selectcheckbox(value, row, index) {
        if(row.isCheck){
            return {
                disabled : false,
                checked : true
            };
            return value;
        }
    }
};

/**
 * 对照
 */
SdsStandLaborderMatch.compare = function (){
    var selected = $('#SdsStandLaborderMatchTable').bootstrapTable('getSelections');
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandLaborderMatch.table.refresh();
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId","");
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("labitemIds","");
        ajax.set("laborderIds",idStr);
        ajax.start();
    }else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

$("#SdsStandLaborderMatchTable").on("check.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandLaborderMatch.table.refresh();
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId","");
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("labitemIds","");
        ajax.set("laborderIds",row.id + ",");
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

$("#SdsStandLaborderMatchTable").on("check-all.bs.table", function (e,dataArr) {
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandLaborderMatch.table.refresh();
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId","");
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("labitemIds","");
        var laborderIds = "";
        for (var i in dataArr){
            laborderIds += dataArr[i].id + ",";
        }
        ajax.set("laborderIds",laborderIds);
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

/**
 * 撤销
 */
$("#SdsStandLaborderMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandLaborderMatch.table.refresh();
        SdsStandLabitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("laborderIds",row.id + ",");
    ajax.set("labitemIds","");
    ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
    ajax.set("sdsStandDicsubId","");
    ajax.start();
});

$("#SdsStandLaborderMatchTable").on("uncheck-all.bs.table", function (e,dataArr) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandLaborderMatch.table.refresh();
        SdsStandLabitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    var laborderIds = "";
    for (var i in dataArr){
        laborderIds += dataArr[i].id + ",";
    }
    ajax.set("laborderIds",laborderIds);
    ajax.set("labitemIds","");
    ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
    ajax.set("sdsStandDicsubId","");
    ajax.start();
});

/**
 * 未对照
 */
SdsStandLaborderMatch.compareNo = function (){
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['compare'] = "noMatch";
        SdsStandLaborderMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}


/**
 * 已对照
 */
SdsStandLaborderMatch.compareYes = function (){
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['compare'] = "match";
        SdsStandLaborderMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsStandLaborderMatch.compareAll = function (){
    if(SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['compare'] = "all";
        SdsStandLaborderMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 自动匹配
 */
SdsStandLaborderMatch.autoCompare = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/autoCompareLabitem", function (data) {
            Sys.success("自动匹配成功!");
            SdsStandLaborderMatch.table.refresh();
        }, function (data) {
            Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否进行自动匹配?",operation);
}

/**
 * 查询标准化_检验项目配置列表
 */
SdsStandLaborderMatch.search = function (compare) {
    var queryData = {};
    queryData['dicId'] = SdsStandDicSub.dicId
    queryData['compare'] = compare;
    queryData['description'] = $("#seachOrderKey").val();
    SdsStandLaborderMatch.table.refresh({query: queryData, pageNumber: 1});
};

/**
 * 查询医嘱关键字
 */
$('#seachOrderKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsStandLaborderMatch.search();
    }
});

/************************************************************
 ********************** 右下表格部分*******************************
 ***********************************************************/
/**
 */

/**
 * 标准化_检验项目配置管理初始化
 */
var SdsStandLabitemMatch = {
    id: "SdsStandLabitemMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsStandLabitemMatch.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter : selectcheckbox},
        {title: '代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '检验项目', field: 'description', align: 'center', valign: 'middle', sortable: true},
        ];
        function selectcheckbox(value, row, index) {
            if(row.isCheck){
                return {
                    disabled : false,
                    checked : true
                };
                return value;
            }
        }
};

/**
 * 检查是否选中
 */
SdsStandLabitemMatch.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsStandLabitemMatch.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增标准化_检验项目配置
 */
SdsStandLabitemMatch.add = function () {
    var index = Sys.open('新增标准化_检验项目配置',Sys.backendPath + '/sds/sdsstandlabitemmatch/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看标准化_检验项目配置详情
 */
SdsStandLabitemMatch.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('标准化_检验项目配置详情',Sys.backendPath + '/sds/sdsstandlabitemmatch/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除标准化_检验项目配置
 */
SdsStandLabitemMatch.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/delete", function (data) {
	            Sys.success("删除成功!");
	            SdsStandLabitemMatch.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsStandLabitemMatchId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除标准化_检验项目配置?",operation);
    }
};

/**
 * 同步检验项目表到标准化-检验项目配置表
 */
SdsStandLabitemMatch.sync = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/sync", function (data) {
            Sys.success("同步成功!");
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("同步失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否同步检验项目到标准化-检验项目配置?",operation);
}

/**
 * 对照
 */
SdsStandLabitemMatch.compare = function (){
    var selected = $('#SdsStandLabitemMatchTable').bootstrapTable('getSelections');
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
        ajax.set("labitemIds",idStr);
        ajax.set("laborderIds","");
        ajax.start();
    }else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

$("#SdsStandLabitemMatchTable").on("check.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    console.log(row)
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            /*SdsStandRisorderMatch.table.refresh();*/
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
        ajax.set("labitemIds",row.id + ",");
        ajax.set("laborderIds","");
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

$("#SdsStandLabitemMatchTable").on("check-all.bs.table", function (e,dataArr) {
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/compare", function (data) {
            Sys.success("对照成功!");
            /*SdsStandRisorderMatch.table.refresh();*/
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
        var labitemIds = "";
        for (var i in dataArr){
            labitemIds += dataArr[i].id + ",";
        }
        ajax.set("labitemIds",labitemIds);
        ajax.set("laborderIds","");
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

/**
 * 撤销
 */
$("#SdsStandLabitemMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandLabitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("laborderIds","");
    ajax.set("labitemIds",row.id + ",");
    ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
    ajax.set("sdsStandDicId","");
    ajax.start();
});

$("#SdsStandLabitemMatchTable").on("uncheck-all.bs.table", function (e,dataArr) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandLabitemMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("laborderIds","");
    var labitemIds = "";
    for (var i in dataArr){
        labitemIds += dataArr[i].id + ",";
    }
    ajax.set("labitemIds",labitemIds);
    ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
    ajax.set("sdsStandDicId","");
    ajax.start();
});

SdsStandLabitemMatch.undo = function (){
    var selected = $('#SdsStandLabitemMatchTable').bootstrapTable('getSelections');
    if(selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        SdsStandLabitemMatch.seItem = selected[0];
        var id = SdsStandLabitemMatch.seItem.id;
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/undo", function (data) {
                Sys.success("撤销成功!");
                SdsStandLabitemMatch.table.refresh();
            }, function (data) {
                Sys.error("撤销失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsStandLabitemMatchId",id);
            ajax.start();
        };
        Sys.confirm("是否撤销对照关系?",operation);
    }
}

/**
 * 未对照
 */
SdsStandLabitemMatch.compareNo = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['compare'] = "noMatch";
        queryData['description'] = $("#seachLabitemKey").val();
        SdsStandLabitemMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}


/**
 * 已对照
 */
SdsStandLabitemMatch.compareYes = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['compare'] = "match";
        queryData['description'] = $("#seachLabitemKey").val();
        SdsStandLabitemMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsStandLabitemMatch.compareAll = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['compare'] = "all";
        queryData['description'] = $("#seachLabitemKey").val();
        SdsStandLabitemMatch.table.refresh({query: queryData, pageNumber: 1});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 自动匹配
 */
SdsStandLabitemMatch.autoCompare = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/autoCompareLabitem", function (data) {
            Sys.success("自动匹配成功!");
            SdsStandLabitemMatch.table.refresh();
        }, function (data) {
            Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否进行自动匹配?",operation);
}

/**
 * 查询标准化_检验项目配置列表
 */
SdsStandLabitemMatch.search = function (compare) {
    var queryData = {};
    queryData['dicId'] = SdsStandDicSub.dicId;
    queryData['dicSubId'] = SdsStandDicSub.dicSubId;
    queryData['labCodeDr'] = $("#labCodeDr").val();
    queryData['compare'] = compare;
    queryData['description'] = $("#seachLabitemKey").val();
    SdsStandLabitemMatch.table.refresh({query: queryData, pageNumber: 1});
};

/**
 * 查询检验项目关键字
 */
$('#seachLabitemKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsStandLabitemMatch.search();
    }
});

$(function () {
    //查询检查字典树
    var ztree = new $ZTree("dicSubTree", Sys.backendPath + "/sds/sdsstanddicsub/tree/" + "检验生化");
    ztree.bindOnClick(SdsStandDicSub.onClickDicSub);
    ztree.init();
    SdsStandDicSub.ztree = ztree;
    fuzzySearch('dicSubTree','#seachKey',null,true);

    //对照的字典颜色改变
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/matchDicsub", function (data) {
        var treeObj = $.fn.zTree.getZTreeObj("dicSubTree");
        for (var i=0;i<data.length;i++){
            var node = treeObj.getNodeByParam("id", data[i]);
            $("#"+node.tId+"_span").css("color",'red');
        }
    }, function (data) {
        Sys.error("渲染失败!" + data.responseJSON.message + "!");
    });
    ajax.start();

    //检验医嘱
    var laborderColunms = SdsStandLaborderMatch.initColumn();
    var laborderTable = new BSTable(SdsStandLaborderMatch.id, Sys.backendPath + "/sds/sdsstandlabitemmatch/listMatchLaborder", laborderColunms);
    laborderTable.setPaginationType("server");
    laborderTable.setPageSize(10);
    laborderTable.setSubHeight(-10);
    laborderTable.setShowExport(false);
    laborderTable.setShowRefresh(false);
    laborderTable.setShowToggle(false);
    laborderTable.setShowColumns(false);
    laborderTable.setQueryParams({"compare":""});
    laborderTable.setQueryParams({"dicId":0});
    SdsStandLaborderMatch.table = laborderTable.init();

    //检验项目
    var defaultColunms = SdsStandLabitemMatch.initColumn();
    var table = new BSTable(SdsStandLabitemMatch.id, Sys.backendPath + "/sds/sdsstandlabitemmatch/listLabitem", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setSubHeight(-10);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.setQueryParams({"compare":""});
    table.setQueryParams({"dicSubId":0});
    SdsStandLabitemMatch.table = table.init();
});
